using Abc.Core.Entities;

using Furion.DatabaseAccessor;
using Furion.DynamicApiController;

using Mapster;

using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;

using System.Threading.Tasks;

namespace Abc.Application.Api
{
    /// <summary>
    /// 用户表
    /// </summary>
    [ApiDescriptionSettings(Name = "User")]
    //[Injection(Proxy = typeof(LogDispatchProxy))]
    [ServiceFilter(typeof(ApiActionFilterAttribute))]
    public class ApiUserService : IDynamicApiController
    {
        private readonly ILogger<ApiUserService> _logger;
        private readonly IRepository<User> _userRepository;

        //private readonly IMemoryCache _memoryCache;
        public ApiUserService(ILogger<ApiUserService> logger
        //, IMemoryCache memoryCache
          , IRepository<User> userRepository
         )
        {
            _logger = logger;
            // _memoryCache = memoryCache;
            //_memoryCache =  App.GetService<IMemoryCache>();
            _userRepository = userRepository;
        }

        /// <summary>
        /// 新增或更新一条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        //[SecurityDefine(SecurityConsts.ROLE_SERVICE_GIVE)]
        //[SecurityDefine, ApiDescriptionSettings(Name = "InsertOrUpdate")]
        public async Task<ApiUserDto> InsertOrUpdate(ApiUserInput input)
        {
            if (input.Id > 0)
            {
                var dbentity = await _userRepository.FindAsync(input.Id);
                input.Adapt(dbentity);
                await _userRepository.UpdateAsync(dbentity);
                return dbentity.Adapt<ApiUserDto>();
            }
            else
            {
                var entryEntity = await _userRepository.InsertAsync(input.Adapt<User>());
                return entryEntity.Entity.Adapt<ApiUserDto>();
            }
        }

        /// <summary>
        /// 删除一条
        /// </summary>
        /// <param name="id"></param>
        //[SecurityDefine(SecurityConsts.ROLE_SERVICE_GIVE)]
        //[SecurityDefine, ApiDescriptionSettings(Name = "Delete")]
        public async Task Delete(int id)
        {
            await _userRepository.DeleteAsync(id);
        }

        /// <summary>
        /// 查询一条[跟踪实体]
        /// </summary>
        /// <param name="id"></param>
        //[SecurityDefine(SecurityConsts.ROLE_SERVICE_GIVE)]
        //[SecurityDefine, ApiDescriptionSettings(Name = "Get")]
        public async Task<ApiUserDto> Get(int id)
        {
            var dbentity = await _userRepository.DetachedEntities.FirstOrDefaultAsync(o => o.Id == id);
            if (dbentity == null) return null;
            return dbentity.Adapt<ApiUserDto>();
        }

        #region 默认未启用

        ///// <summary>
        ///// 新增一条
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        ////[SecurityDefine(SecurityConsts.ROLE_SERVICE_GIVE)]
        ////[SecurityDefine, ApiDescriptionSettings(Name = "Insert")]
        //public async Task<ApiUserDto> Insert(ApiUserInput input)
        //{
        //    var entryEntity = await _userRepository.InsertAsync(input.Adapt<User>());
        //    return entryEntity.Entity.Adapt<ApiUserDto>();
        //}
        //
        ///// <summary>
        ///// 更新一条
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        ////[SecurityDefine(SecurityConsts.ROLE_SERVICE_GIVE)]
        ////[SecurityDefine, ApiDescriptionSettings(Name = "Update")]
        //public async Task Update(int id,ApiUserInput input)
        //{
        //    var dbentity = await _userRepository.FindAsync(id);
        //    input.Adapt(dbentity);
        //    await _userRepository.UpdateAsync(dbentity);
        //}
        //
        //
        //
        //
        //
        ///// <summary>
        ///// 查询所有
        ///// </summary>
        ///// <returns></returns>
        ////[SecurityDefine(SecurityConsts.ROLE_SERVICE_GIVE)]
        ////[SecurityDefine, ApiDescriptionSettings(Name = "GetAll")]
        //public async Task<List<ApiUserDto>> GetAll()
        //{
        //    var entities = _userRepository.DetachedEntities.AsQueryable().OrderByDescending(o => o.Id).ProjectToType<ApiUserDto>();
        //    return await entities.ToListAsync();
        //}
        ///// <summary>
        ///// 分页查询
        ///// </summary>
        ///// <param name="searchQuery"></param>
        ///// <returns></returns>
        ////[SecurityDefine(SecurityConsts.ROLE_SERVICE_GIVE)]
        ////[SecurityDefine, ApiDescriptionSettings(Name = "GetAllByPage")]
        //public async Task<PagedList<ApiUserDto>> GetAllByPage(ApiUserQuery searchQuery)
        //{
        //    //var queryable= _userRepository.DetachedEntities.AsQueryable(tracking:false);
        //    var pageResult = _userRepository.DetachedEntities.AsQueryable().OrderByDescending(o => o.Id).ProjectToType<ApiUserDto>();
        //
        //    return await pageResult.ToPagedListAsync(searchQuery.Page, searchQuery.Limit);
        //}
        ///// <summary>
        ///// 分页查询
        ///// </summary>
        ///// <param name="searchQuery"></param>
        ///// <returns></returns>
        ////[SecurityDefine(SecurityConsts.ROLE_SERVICE_GIVE)]
        ////[SecurityDefine, ApiDescriptionSettings(Name = "GetAllByPageToLayui")]
        //public async Task<PagedListToLayui<ApiUserDto>> GetAllByPageToLayui(ApiUserQuery searchQuery)
        //{
        //    //var queryable= _userRepository.DetachedEntities.AsQueryable(tracking:false);
        //    var pageResult = _userRepository.DetachedEntities.AsQueryable().OrderByDescending(o => o.Id).ProjectToType<ApiUserDto>();
        //
        //    return await pageResult.ToPagedListToLayuiAsync(searchQuery.Page, searchQuery.Limit);
        //}

        #endregion 默认未启用
    }
}